var express = require('express');
var router = express.Router();
var UserModel = require('../tool/model/user.js');
router.get('/', (req, res, next)=> {
    res.send('hello world')
  });
router.post('/register',async function(req, res, next) {
    let obj = req.body;
    obj.password = md5(obj.password)
    var user = new UserModel ({
        userName:obj.username,
        userPassword:obj.password,
        userEmail:obj.email,//以邮箱作为该用户的管理标识，若更该则更新此字段
        userPhone:obj.phone
      })
      await user.save(function(err){
        if(err){
            console.log(err);
            return;
        }else{
          res.redirect('/login')
        }
    })
  });
  router.get('/login',async function(req, res, next) {
    // let obj = req.body; // {userName: '', userPassword:''}
    // obj.userPassword = md5(obj.userPassword)
    console.log('obj')
    res.send('obj')
    // await UserModel.find(obj,function(err,docs){
    //   if(err){
    //     console.log(err);
    //     return;
    // }
    // console.log(docs[0].userRoots,docs[0].userID)
    //   if (docs.length === 0) {
    //         res.send('并不是管理员')
    //       } else {
    //         res.cookie('id',docs[0].userID,{maxAge: 3600000*24,singed:true, httpOnly: true})
    //         res.cookie('isLogin', 'ok')
    //         res.cookie('root',docs[0].userRoots)

    //       }
    //       return;
    // })
  });
  module.exports = router;